package com.dream.myqiyi.ui;


import com.dream.myqiyi.BaseApp;
import com.dream.myqiyi.Global;
import com.dream.myqiyi.R;
import com.dream.myqiyi.http.NoteSync;
import com.dream.myqiyi.service.ApplicationContext;
import com.dream.myqiyi.service.NoteService;
import com.dream.myqiyi.service.NotebookService;
import com.dream.myqiyi.service.TagService;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends BaseActivity {

	// 功能控件
	private TextView usernameTv = null;
	private TableRow main_page_row_note;
	private TableRow main_page_row_notebook;
	private TableRow main_page_row_tag;
	private TableRow main_page_row_address;
	
	private LinearLayout newNoteLayout;
	private LinearLayout newCameraNoteLayout;
	private LinearLayout newRecordNoteLayout;
	private LinearLayout newAttachmentNoteLayout;
	
	private TextView tvAllNoteCount;
	private TextView tvAllNotebookCount;
	private TextView tvAllTagCount;
	private TextView tvAllAddressCount;

	private static final int DIALOG_SYNC = 0;
	//private Button mRightBtn;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
         //启动activity时不自动弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        initUI();

        upadteUIData();
    }
	
	@Override 
	protected void onResume() {  
		super.onResume(); 

		upadteUIData(); 
	} 

	private void initUI() {
		usernameTv = (TextView)findViewById(R.id.main_username_tv);
		ImageView userIv = (ImageView)findViewById(R.id.main_user_iv);
		userIv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(MainActivity.this, AccountActivity.class);
				startActivity(intent);


			}

		}) ;
		ImageView syncIv = (ImageView)findViewById(R.id.main_sync_iv);
		syncIv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				new SyncTask().execute("");
			}
		});

		main_page_row_note=(TableRow) this.findViewById(R.id.main_page_row_note);//note
		main_page_row_note.setOnClickListener(new btclick());
		main_page_row_notebook=(TableRow) this.findViewById(R.id.main_page_row_notebook);//notebook
		main_page_row_notebook.setOnClickListener(new btclick());
		main_page_row_tag=(TableRow) this.findViewById(R.id.main_page_row_tag);//tag
		main_page_row_tag.setOnClickListener(new btclick());
		main_page_row_address=(TableRow) this.findViewById(R.id.main_page_row_address);//address
		main_page_row_address.setOnClickListener(new btclick());
		
		tvAllNoteCount = (TextView) this.findViewById(R.id.all_note_count_tv);
		tvAllNotebookCount = (TextView) this.findViewById(R.id.all_notebook_count_tv);
		tvAllTagCount = (TextView) this.findViewById(R.id.all_tag_count_tv);
		tvAllAddressCount = (TextView) this.findViewById(R.id.all_address_count_tv);

		
		newNoteLayout = (LinearLayout)findViewById(R.id.main_new_note);
		newNoteLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("note_type", NoteListActivity.ALL_NOTE_TYPE);
				//bundle.putString("guid", notebookGuid);
				Intent intent = new Intent(MainActivity.this, NoteViewActivity.class);
				intent.putExtras(bundle);

				startActivity(intent);
			}
			
		});
		newCameraNoteLayout = (LinearLayout)findViewById(R.id.main_new_camera);
		newCameraNoteLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putInt("note_type", NoteListActivity.ALL_NOTE_TYPE);
				bundle.putString("new_mode", "camera");
				//bundle.putString("guid", notebookGuid);
				Intent intent = new Intent(MainActivity.this, NoteViewActivity.class);
				intent.putExtras(bundle);

				startActivity(intent);
			}
			
		});
		newRecordNoteLayout = (LinearLayout)findViewById(R.id.main_new_recorder);
		newRecordNoteLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Bundle bundle = new Bundle();
				bundle.putInt("note_type", NoteListActivity.ALL_NOTE_TYPE);
				bundle.putString("new_mode", "recorder");
				//bundle.putString("guid", notebookGuid);
				Intent intent = new Intent(MainActivity.this, NoteViewActivity.class);
				intent.putExtras(bundle);

				startActivity(intent);
			}
			
		});
		newAttachmentNoteLayout = (LinearLayout)findViewById(R.id.main_new_attachment);
		newAttachmentNoteLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				BaseApp.showToast("暂未开放");
			}
			
		});


	}
	
	private void upadteUIData() {
        SharedPreferences sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        usernameTv.setText(sp.getString("username", ""));

		tvAllNoteCount.setText(Integer.toString(0));
		tvAllNotebookCount.setText(Integer.toString(0));
		tvAllTagCount.setText(Integer.toString(0));
		tvAllAddressCount.setText(Integer.toString(0));
		
		new GetNoteCountTask().execute("");
	}
	
	private class GetNoteCountTask extends AsyncTask<String, String, String> {
		private int allNoteCount = 0;
		private int allNotebookCount = 0;
		private int allTagCount = 0;
		private int allAddressNoteCount = 0;
	        
		public String doInBackground(String... params) {		 
			// 获取数据
			allNoteCount = Global.noteSrv.getAllActiveNoteCount();
			allNotebookCount = Global.notebookSrv.getAllNotebookCount();
			allTagCount = Global.tagSrv.getAllTagCount();
			allAddressNoteCount = Global.noteSrv.getActiveAddressNoteCount();
			
			return "";
		}
		@Override
		public void onPostExecute(String Re) {
			
			tvAllNoteCount.setText(Integer.toString(allNoteCount));
			tvAllNotebookCount.setText(Integer.toString(allNotebookCount));
			tvAllTagCount.setText(Integer.toString(allTagCount));
			tvAllAddressCount.setText(Integer.toString(allAddressNoteCount));
		}
	}

	public class btclick implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent = null;
			switch (v.getId()) {
			case R.id.main_page_row_note :
				intent = new Intent(MainActivity.this, NoteListActivity.class);
				startActivity(intent);
				break;
			case R.id.main_page_row_notebook :
				intent = new Intent(MainActivity.this, NotebookListActivity.class);
				startActivity(intent);
				break;
			case R.id.main_page_row_tag :
				intent = new Intent(MainActivity.this, TagListActivity.class);
				startActivity(intent);
				break;
			case R.id.main_page_row_address :
				intent = new Intent(MainActivity.this, BMapAddressView.class);
				startActivity(intent);
				break;
			}
		}

	}

	private class SyncTask extends AsyncTask<String, String, String> {
		public String doInBackground(String... params) {
			NoteSync noteSync = (NoteSync)ApplicationContext.getSingleton().getBean("NoteSync");
			noteSync.getNotebooks();
			noteSync.getTags();
			noteSync.getNotes();
			return "";
		}

		@Override
		protected void onPreExecute() {
			showDialog(DIALOG_SYNC);
		}

		@Override
		public void onPostExecute(String Re) {
			removeDialog(DIALOG_SYNC);
			
            //更新界面数据
			upadteUIData();
		}
						
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_SYNC: {
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage("正在同步...请稍候");
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			return dialog;
		}
			
		}
		return null;
	}



}